//
//  CommentRefModel.m
//  ZhiHuDaily
//
//  Created by albert on 16/8/17.
//  Copyright © 2016年 albert. All rights reserved.
//

#import "CommentRefModel.h"

@interface CommentRefModel ()
{
    CGFloat _cmtContentRefH;
}
@end

@implementation CommentRefModel

+ (NSDictionary *)mj_replacedKeyFromPropertyName{
    return @{
             @"ID" : @"id"
             };
}


- (CGFloat)cmtContentRefH{
    
    if (!_cmtContentRefH) {
        
        UIFont *contentRefFont = [UIFont systemFontOfSize:14];
        
        // 展开收起按钮的显示和隐藏
        //计算文字的高度
        CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 30 - 20 - 8, MAXFLOAT);
        _cmtContentRefH = [self.contentRefAttr boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size.height;
        
        if (_cmtContentRefH - contentRefFont.lineHeight <= 5) {
            
            if ([self containChinese:self.author] || [self containChinese:self.content]) {
                
                _cmtContentRefH -= 5;
                
                // 1.创建可变属性字符串 self.refName” 显示黑色加粗
                NSMutableAttributedString *attrMstr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"//%@:  ", self.author] attributes:@{NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont boldSystemFontOfSize:13]}];
                
                // 2.创建不可变属性字符串 reply_to.content 显示灰色
                NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:self.content attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14], NSForegroundColorAttributeName : JKColor(108, 108, 108),}];
                
                // 3.可变字符串拼接不可变字符串
                [attrMstr appendAttributedString:attrStr];
                
                _contentRefAttr = attrMstr;
                
                // 1.创建可变属性字符串 self.refName” 显示黑色加粗
                NSMutableAttributedString *attrMstr1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"//%@:  ", self.author] attributes:@{NSForegroundColorAttributeName : JKColor(186, 186, 186), NSFontAttributeName : [UIFont boldSystemFontOfSize:13]}];
                
                // 2.创建不可变属性字符串 reply_to.content 显示灰色
                NSAttributedString *attrStr1 = [[NSAttributedString alloc] initWithString:self.content attributes:@{NSForegroundColorAttributeName : JKColor(118, 118, 118)}];
                
                // 3.可变字符串拼接不可变字符串
                [attrMstr1 appendAttributedString:attrStr1];
                _contentRefNightAttr = attrMstr1;
            }
        }
        
        _cmtContentRefH = ceil(_cmtContentRefH);
    }
    
    return _cmtContentRefH;
}

//判断如果包含中文
- (BOOL)containChinese:(NSString *)str {
    
    for (int i = 0; i < [str length]; i++) {
        
        int a = [str characterAtIndex:i];
        
        if (a > 0x4e00 && a < 0x9fff) {
            
            return YES;
        }
    }
    return NO;
}

- (NSAttributedString *)contentRefAttr{
    
    if (!_contentRefAttr) {
        
        NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc] init];
        para.lineSpacing = 5;
        
        // 1.创建可变属性字符串 self.refName” 显示黑色加粗
        NSMutableAttributedString *attrMstr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"//%@:  ", self.author] attributes:@{NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont boldSystemFontOfSize:13], NSParagraphStyleAttributeName : para}];
        
        // 2.创建不可变属性字符串 reply_to.content 显示灰色
        NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:self.content attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14], NSForegroundColorAttributeName : JKColor(108, 108, 108), NSParagraphStyleAttributeName : para}];
        
        // 3.可变字符串拼接不可变字符串
        [attrMstr appendAttributedString:attrStr];
        _contentRefAttr = attrMstr;
    }
    return _contentRefAttr;
}

- (NSAttributedString *)contentRefNightAttr{
    
    if (!_contentRefNightAttr) {
        
        NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc] init];
        para.lineSpacing = 5;
        
        // 1.创建可变属性字符串 self.refName” 显示黑色加粗
        NSMutableAttributedString *attrMstr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"//%@:  ", self.author] attributes:@{NSForegroundColorAttributeName : JKColor(186, 186, 186), NSFontAttributeName : [UIFont boldSystemFontOfSize:13], NSParagraphStyleAttributeName : para}];
        
        // 2.创建不可变属性字符串 reply_to.content 显示灰色
        NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:self.content attributes:@{NSForegroundColorAttributeName : JKColor(118, 118, 118), NSParagraphStyleAttributeName : para}];
        
        // 3.可变字符串拼接不可变字符串
        [attrMstr appendAttributedString:attrStr];
        _contentRefNightAttr = attrMstr;
    }
    return _contentRefNightAttr;
}
@end
